//
//  FaceCompareViewController.m
//  FaceDiscernmentDemo
//
//  Created by mac on 2019/2/22.
//  Copyright © 2019年 mac. All rights reserved.
//

#import "FaceCompareViewController.h"
#import "FaceScanViewController.h"
#import "UIImage+Additions.h"


@interface FaceCompareViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic,strong) UIButton *photoBtnA;
@property (nonatomic,strong) UIButton *photoBtnB;
@property (nonatomic,strong) UIImageView *photoImageA;
@property (nonatomic,strong) UIImageView *photoImageB;
@property (nonatomic,strong) UIButton *commitBtn;
@property (nonatomic,strong) UILabel *tipsLabel;

@property (nonatomic, readwrite, weak) UIImageView *selectImageView;


@end

@implementation FaceCompareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"人脸对比";
    
    [self.commitBtn setBackgroundImage:[UIImage imageWithColor:[UIColor orangeColor] withSize:self.commitBtn.bounds.size] forState:UIControlStateNormal];
    
    [self setFaceCompareUI];
    // Do any additional setup after loading the view.
}

#pragma mark -- Action
- (void)photoButton:(UIButton *)sender{
    __weak typeof(self) weakSelf = self;
    if (sender.tag == 0) {
        self.selectImageView = self.photoImageA;
        UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.videoQuality=UIImagePickerControllerQualityTypeLow;
        imagePickerController.allowsEditing = NO;
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [weakSelf presentViewController:imagePickerController animated:YES completion:nil];
    }else{
        self.selectImageView = self.photoImageB;
        FaceScanViewController *faceScan = [[FaceScanViewController alloc]init];
        faceScan.successCompletion = ^(NSDictionary * _Nonnull imagesDict, UIImage * _Nonnull originImage) {
            weakSelf.selectImageView.hidden = NO;
            weakSelf.selectImageView.image = originImage;
        };
        [weakSelf presentViewController:faceScan animated:YES completion:nil];
    }
}

- (void)commitButton:(UIButton *)sender{
    if (self.photoImageA.hidden || self.photoImageB.hidden) {
        self.tipsLabel.text = @"请上传2张人脸照片";
        return;
    }
    self.tipsLabel.text = @"对比中...";
    NSArray* parmC = @[@{@"image":[self.photoImageA.image base64EncodedString],@"image_type": @"BASE64"},
                      @{@"image":[self.photoImageB.image base64EncodedString],@"image_type": @"BASE64"}];
//    NSDictionary *parmA = @{@"image":[self.photoImageA.image base64EncodedString],@"image_type": @"BASE64"};
//    NSDictionary *parmB = @{@"image":[self.photoImageB.image base64EncodedString],@"image_type": @"BASE64"};
//    NSMutableDictionary *parm = [[NSMutableDictionary alloc]init];
//    [parm addEntriesFromDictionary:parmA];
//    [parm addEntriesFromDictionary:parmB];
//@"access_token":[FaceAppToolHandle shareToolHandle].accessToken
    
    [WebConnect postDataWithPath:[NSString stringWithFormat:@"%@?access_token=%@",Baidu_getFacesetMatchURL,[FaceAppToolHandle shareToolHandle].accessToken] parameters:parmC completion:^(NSError *error, id resultObject) {
        if (error == nil) {
            NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:resultObject options:NSJSONReadingAllowFragments error:nil];
            if ([dict[@"error_code"] intValue] == 0) {
                self.tipsLabel.text = [NSString stringWithFormat:@"相似度分值：%0.2f",[dict[@"result"][@"score"] floatValue]];
            } else {
                self.tipsLabel.text = dict[@"error_msg"]?[NSString stringWithFormat:@"错误信息：%@",dict[@"error_msg"]]:@"网络请求错误";
            }
            NSLog(@"dict = %@",dict);
        } else {
            self.tipsLabel.text = @"网络请求失败";
        }
    }];
    
//    [WebConnect webPOSTConnectWithWithStrUrl:[NSString stringWithFormat:@"%@?access_token=%@",Baidu_getFacesetMatchURL,[FaceAppToolHandle shareToolHandle].accessToken] pramaters:parmC success:^(id responseObject) {
//        NSLog(@"人脸对比=%@",responseObject);
//        if ([dict[@"error_code"] intValue] == 0) {
//            self.tipsLabel.text = [NSString stringWithFormat:@"相似度分值：%0.2f",[dict[@"result"][@"score"] floatValue]];
//        } else {
//            self.tipsLabel.text = dict[@"error_msg"]?[NSString stringWithFormat:@"错误信息：%@",dict[@"error_msg"]]:@"网络请求错误";
//        }
//    } failure:^(id failure) {
//        NSLog(@"失败=%@",failure);
//    }];
    
}

#pragma mark -- UI
- (void)setFaceCompareUI{
    _photoImageA = [[UIImageView alloc]init];
    _photoImageA.image = [UIImage imageNamed:@"add_photo_h"];
    [self.view addSubview:_photoImageA];
    [_photoImageA mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(40);
        make.top.mas_equalTo(self.view.mas_top).offset(100);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(200);
    }];

    _photoImageB = [[UIImageView alloc]init];
    _photoImageB.image = [UIImage imageNamed:@"add_photo_h"];
    [self.view addSubview:_photoImageB];
    [_photoImageB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view.mas_right).offset(-40);
        make.top.mas_equalTo(self.view.mas_top).offset(100);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(200);
    }];
    
    //
    _photoBtnA = [[UIButton alloc]init];
    [_photoBtnA setTitle:@"选择图片" forState:UIControlStateNormal];
    _photoBtnA.backgroundColor = [UIColor grayColor];
    [_photoBtnA addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside];
    _photoBtnA.tag = 0;
    [self.view addSubview:_photoBtnA];
    [_photoBtnA mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(40);
        make.top.mas_equalTo(self.photoImageA.mas_bottom).offset(30);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(35);
    }];
    
    _photoBtnB = [[UIButton alloc]init];
    [_photoBtnB setTitle:@"实时采集" forState:UIControlStateNormal];
    _photoBtnB.backgroundColor = [UIColor grayColor];
    [_photoBtnB addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside];
    _photoBtnB.tag = 1;
    [self.view addSubview:_photoBtnB];
    [_photoBtnB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view.mas_right).offset(-40);
        make.top.mas_equalTo(self.photoImageB.mas_bottom).offset(30);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(35);
    }];
    
    //、、、、、、
    _tipsLabel = [[UILabel alloc]init];
    _tipsLabel.text = @"请传入进行对比的图片";
    _tipsLabel.textColor = [UIColor blackColor];
    _tipsLabel.textAlignment = NSTextAlignmentCenter;
    _tipsLabel.font = [UIFont systemFontOfSize:13];
    _tipsLabel.numberOfLines = 2;
    [self.view addSubview:_tipsLabel];
    [_tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.top.mas_equalTo(self.photoBtnB.mas_bottom).offset(100);
        make.height.mas_equalTo(40);
    }];
    
    _commitBtn = [[UIButton alloc]init];
    [_commitBtn setTitle:@"开始对比" forState:UIControlStateNormal];
    [_commitBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _commitBtn.backgroundColor = [UIColor orangeColor];
    [_commitBtn addTarget:self action:@selector(commitButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_commitBtn];
    [_commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.tipsLabel.mas_bottom).offset(5);
        make.width.mas_equalTo(220);
        make.height.mas_equalTo(40);
    }];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        self.selectImageView.hidden = NO;
        self.selectImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    //取消选择的设置类型
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

@end
